package com.example.xinhuaxin.controller;

import com.example.xinhuaxin.entity.User;
import com.example.xinhuaxin.service.impl.CardServiceImpl;
import com.example.xinhuaxin.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @description:
 * @author: YancyZhang
 * @time: 2022/6/8 10:30
 */
@Controller//RestController返回实体对象，Controller返回页面
public class CardController {
    @Autowired
    CardServiceImpl cardService;
    @Autowired
    UserServiceImpl userService;

    @RequestMapping("/")
    public String index(Model model){
        model.addAttribute("cardList",cardService.getCardList());
        return "index";
    }
    //    申请银行卡
    @RequestMapping("/applyCard")
    public String applyCard(HttpServletRequest req, Model model){
        HttpSession session = req.getSession();
        if(session.getAttribute("card_system_user_id")!=null){
            //根据用户id获取对应信息
            User u = userService.getUserInfo(session.getAttribute("card_system_user_id").toString());
            model.addAttribute("user_info", u);

            model.addAttribute("card_info",cardService.getCardInfo(Integer.parseInt(req.getParameter("id"))));

            return "applyCard";
        }
        model.addAttribute("msg","暂未登录，请先登录！");
        return "login";
    }


    // 完成银行卡绑定业务
    @RequestMapping("/completeCardBind")
    @ResponseBody
    public Boolean completeCardBind(HttpServletRequest req){
        try{
            cardService.bindCard(req.getParameter("user_id"),  Integer.parseInt(req.getParameter("card_id")));
            return true;
        }catch (Exception e){
            return false;
        }
        //绑定成功 跳转到个人信息页面
//        return "redirect:userinfo";
    }

//    银行卡详情
    @RequestMapping("/cardDetail")
    public String toCardDetail(HttpServletRequest req, Model model){
        model.addAttribute("bindId", cardService.getCardDetail(req.getParameter("userId"),Integer.parseInt(req.getParameter("cardId"))));
        model.addAttribute("cardInfo",cardService.getCardInfo(Integer.parseInt(req.getParameter("cardId"))));
        return "cardDetail";
    }
//    注销银行卡
    @RequestMapping("/closeAccount")
    @ResponseBody
    public Boolean closeAccount(HttpServletRequest req, Model model){
        if(cardService.closeAccount(req.getParameter("id"))){
            return true;
        }
        model.addAttribute("msg", "注销失败！");
        return false;
    }
}